//
//  FJC_SearchViewController.m
//  滴滴快拍
//
//  Created by dllo on 16/6/1.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FJC_SearchViewController.h"
#import "FJC_SearchModel.h"
#import "FJC_SearchColleactionviewCell.h"
#import "FJC_CollectionHeaderView.h"
#import "AGLChannelCollectionCell.h"
#import "Header.h"
#import "Frame.h"

@interface FJC_SearchViewController ()<
UISearchBarDelegate,
UICollectionViewDelegate,
UICollectionViewDataSource
>

@property (nonatomic, strong)UISearchController *searchController;
@property (nonatomic, strong)NSMutableArray *upCollectArr;
@property (nonatomic, strong)NSMutableArray *downCollectArr;
@property (nonatomic, strong)UICollectionView *upCollectionView;
@property (nonatomic, strong)UICollectionView *downCollectionView;

@end

@implementation FJC_SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createData];
    [self createCollectView];
    [self createSearch];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    NSString *upUrlStr = [NSString stringWithFormat:@"http://api.miaopai.com/m/search-user.json?&keyword=%@", [self.searchController.searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    AFHTTPSessionManager *managerFirst = [AFHTTPSessionManager manager];
    [managerFirst GET:upUrlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *upArr = responseObject[@"result"];
        self.upCollectArr = [NSMutableArray array];
        for (NSDictionary *tempDic in upArr) {
            FJC_SearchModel *tempModel = [FJC_SearchModel new];
            [tempModel setValuesForKeysWithDictionary:tempDic];
            [self.upCollectArr addObject:tempModel];
        }
        [self.upCollectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    
    
    NSString *downUrlStr = [NSString stringWithFormat:@"http://api.miaopai.com/m/search-channel.json?&keyword=%@", [self.searchController.searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    AFHTTPSessionManager * managerSecond = [AFHTTPSessionManager manager];
    [managerSecond GET:downUrlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *downArr = responseObject[@"result"];
        self.downCollectArr = [NSMutableArray array];
        for (NSDictionary *tempDic in downArr) {
            AGLChannel *tempModel = [AGLChannel new];
            [tempModel setValuesForKeysWithDictionary:tempDic];
            [self.downCollectArr addObject:tempModel];
        }
        [self.downCollectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}


- (void)createData {
    
    NSString *upUrlStr = [NSString stringWithFormat:@"http://api.miaopai.com/m/search-user.json?&keyword=limyoona"];
    AFHTTPSessionManager *managerFirst = [AFHTTPSessionManager manager];
    [managerFirst GET:upUrlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *upArr = responseObject[@"result"];
        self.upCollectArr = [NSMutableArray array];
        for (NSDictionary *tempDic in upArr) {
            FJC_SearchModel *tempModel = [FJC_SearchModel new];
            [tempModel setValuesForKeysWithDictionary:tempDic];
            [self.upCollectArr addObject:tempModel];
        }
        [self.upCollectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    
    
    NSString *downUrlStr = [NSString stringWithFormat:@"http://api.miaopai.com/m/search-channel.json?&keyword=limyoona"];
    AFHTTPSessionManager * managerSecond = [AFHTTPSessionManager manager];
    [managerSecond GET:downUrlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *downArr = responseObject[@"result"];
        self.downCollectArr = [NSMutableArray array];
        for (NSDictionary *tempDic in downArr) {
            AGLChannel *tempModel = [AGLChannel new];
            [tempModel setValuesForKeysWithDictionary:tempDic];
            [self.downCollectArr addObject:tempModel];
        }
        [self.downCollectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

}

#pragma mark 创建搜索栏
- (void)createSearch{
    
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    // 点击搜索栏时隐藏导航栏
    self.searchController.hidesNavigationBarDuringPresentation = NO;
    self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44);
    [self.searchController.searchBar sizeToFit];
    self.searchController.searchBar.placeholder = @"搜索宝贝";
    self.searchController.searchBar.tintColor = [UIColor blackColor];
    self.searchController.searchBar.delegate = self;
    self.navigationItem.titleView = self.searchController.searchBar;
    
}

- (void)createCollectView {
    
#pragma mark 下面视频
    UICollectionViewFlowLayout *downFlow = [[UICollectionViewFlowLayout alloc] init];
    downFlow.itemSize = CGSizeMake(SCREEN_WIDTH / 2 - 20, 200);
    downFlow.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 40);
    downFlow.scrollDirection = UICollectionViewScrollDirectionVertical;
    downFlow.minimumInteritemSpacing = 5;
    downFlow.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    self.downCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 113) collectionViewLayout:downFlow];
    self.downCollectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.downCollectionView];
    
#pragma mark 创建用户
    UICollectionViewFlowLayout *upFlow = [[UICollectionViewFlowLayout alloc] init];
    upFlow.itemSize = CGSizeMake(SCREEN_WIDTH / 5, SCREEN_WIDTH / 5);
    upFlow.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 20);
    upFlow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    upFlow.minimumInteritemSpacing = 5;
    upFlow.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    
    self.upCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, -130, SCREEN_WIDTH, SCREEN_WIDTH / 5 + 50) collectionViewLayout:upFlow];
    self.upCollectionView.backgroundColor = [UIColor clearColor];
    [self.downCollectionView addSubview:self.upCollectionView];

    
    self.upCollectionView.showsHorizontalScrollIndicator = NO;
    self.upCollectionView.delegate = self;
    self.upCollectionView.dataSource = self;
    
    self.downCollectionView.showsVerticalScrollIndicator = NO;
    self.downCollectionView.delegate = self;
    self.downCollectionView.dataSource = self;
    self.downCollectionView.contentInset = UIEdgeInsetsMake(150, 0, 0, 0);
    
    [self.upCollectionView registerClass:[FJC_SearchColleactionviewCell class] forCellWithReuseIdentifier:@"searchUpCollectionReuse"];
    
    [self.downCollectionView registerClass:[AGLChannelCollectionCell class] forCellWithReuseIdentifier:@"searchDownCollectionReuse"];
    
    [self.upCollectionView registerClass:[FJC_CollectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    
    [self.downCollectionView registerClass:[FJC_CollectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
}

#pragma mark 头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
            FJC_CollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
    if (collectionView == _downCollectionView) {
        headerView.titleLabel.text = @"视频";
        return headerView;
    }else if (collectionView == _upCollectionView) {
        headerView.titleLabel.text = @"用户";
        return headerView;

    }
    
    return nil;
   
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (collectionView == _upCollectionView) {
        return CGSizeMake(40, 0);
    }else {
         return CGSizeMake(SCREEN_WIDTH, 20);
    }
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (collectionView == _upCollectionView) {
        return self.upCollectArr.count;
    }else if (collectionView == _downCollectionView) {
        return self.downCollectArr.count;
    }
    return 0;
}
#pragma mark cell方法

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (collectionView == _upCollectionView) {
        FJC_SearchColleactionviewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"searchUpCollectionReuse" forIndexPath:indexPath];
        FJC_SearchModel *upMod = self.upCollectArr[indexPath.row];
        [cell.userImageView setImageWithURL:[NSURL URLWithString:upMod.icon]];
        cell.nameLabel.text = upMod.nick;
        return cell;
        
    }else if (collectionView == _downCollectionView) {
        
        AGLChannelCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"searchDownCollectionReuse" forIndexPath:indexPath];
        
        AGLChannel *downMod = [self.downCollectArr objectAtIndex:indexPath.row];
        cell.channel = downMod;
        return cell;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView == _downCollectionView) {
        AGLChannel *channel = self.downCollectArr[indexPath.item];
        
        VideoDetailViewController *vidoeDetailVC = [[VideoDetailViewController alloc] init];
        vidoeDetailVC.scid = channel.scid;
        vidoeDetailVC.videoURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://gslb.miaopai.com/stream/%@.mp4", channel.scid]];
        vidoeDetailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vidoeDetailVC animated:YES];
        vidoeDetailVC.hidesBottomBarWhenPushed = YES;
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
